layui.use(['layer', 'form', 'jquery'], function() {
       let $ = layui.jquery,
        layer = layui.layer,
        form = layui.form;
        getCookie();

    //监听提交
    form.on('submit(formDemo)', function(data){
        let index = layer.load();
        let url = "/doLogin";
        //记住密码则设置cookie
        let rememberMe = $("input[name='rememberme']").is(':checked');
        $.ajax({
                type:"post",
                url:"/auth/doLogin",
                dataType: 'json',
                data:{username:data.field.loginName,password:data.field.password,captcha:data.field.captcha,rememberMe:rememberMe},
                async:false,
                success:function (e) {
                    layer.closeAll();
                    if (e.code===0){
                        layer.msg(e.code);
                        document.location.href = "/";
                    }else {
                        layer.msg(e.msg);
                        $(".imgcode").click();
                        $(".chptcha").val("");
                    }
                },
                error:function () {
                    layer.closeAll();
                    layer.msg("服务器错误");
                }
            }
        );
        return false;
    });
})
//设置cookie
function setCookie() {
    let d = new Date();
    d.setTime(d.getTime()+(1*24*60*60*1000));
    let expires = "expires="+d.toGMTString();
    if($("input[name='rememberme']").is(':checked')){
        $("input[iscookie='true']").each(function() {
        alert("-------------------------------------------"+"."+this.name)
            $.cookie(this.name, $("."+this.name).val() ,expires);
            $.cookie("COOKIE_NAME","true", expires);
        });
    }else {
        $("input[iscookie='true']").each(function() {
            $.cookie(this.name,null);
            $.cookie("COOKIE_NAME",null);
        });
    }
}
//获取cookie
function getCookie(){
    console.log("===================================")
    let COOKIE_NAME=$.cookie("COOKIE_NAME");
    if (COOKIE_NAME !=null) {
        $("input[iscookie='true']").each(function() {
            $($("."+this.name).val( $.cookie(this.name)));
           /* if("admin" == $.cookie(this.name)) {
                $(".imgcode").click();
            } else {
                $(".password").val("");
                $(".password").focus();
            }*/
        })
        $("input[type='checkbox']").prop("checked", true);
        $("#rememberme").attr("checked", true);
        $("#rememberme").val("1");
        layui.form.render();
    }else {
        $("input[type='checkbox']").prop("checked", true);
        $("#rememberme").attr("checked", false);
        $("#rememberme").val("0");
        $(".imgcode").click();
        layui.form.render();
    }


}
/*验证码*/
$(function () {
    $(".imgcode").click(function () {
        let url = "/public/imgCaptcha.png?noCache="+Math.random();
        $(".imgcode").attr("src",url);
    });
});


